﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddService : System.Web.UI.Page
    {
        private readonly ServiceBiz serviceBiz = new ServiceBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            string script = @"<script>
            function OpenPopup() {
                popup('/Pages/Views/PopupListContacts.aspx');
            }
            function OpenPopup1() {
                popup('PopupListContacts1.aspx');
            }
            function popup(url) {

                var width = 500;
                var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) { newwin.focus() }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "Mast", script);
            }

            if (Session["contact"] != null || Session["contact1"] != null)
            {
                var o = Session["contact"];
                if (o != null) txtContact.Text = o.ToString();
                var s = Session["contact1"];
                if (s != null) txtContact1.Text = Session["contact1"].ToString();
            }

            if (!IsPostBack)
            {

            }
        }

        protected void CheckTabs()
        {
            if (cboServiceType.SelectedIndex == 0)
            {
                pnlContract.Visible = false;
                pnlFunding.Visible = false;
                LinkContract.Text = string.Empty;
                LinkContract.NavigateUrl = string.Empty;
            }

            if (cboServiceType.SelectedIndex == 1)
            {
                pnlFunding.Visible = false;
                pnlContract.Visible = true;
                LinkContract.NavigateUrl = "#pnlContract";
                LinkContract.Text = "Contract";
                txtFundingStart.Text = string.Empty;
                txtFundingEnd.Text = string.Empty;
                txtFundraisingRequired.Text = string.Empty;
                txtFDonationDate.Text = string.Empty;
            }

            if (cboServiceType.SelectedIndex == 2)
            {
                pnlContract.Visible = false;
                pnlFunding.Visible = true;
                LinkContract.NavigateUrl = "#pnlFunding";
                LinkContract.Text = "Funding";
            }
        }

        protected bool CheckNameExist()
        {
            var listService = serviceBiz.GetAll();
            foreach (var itemService in listService)
            {
                if (txtServiceName.Text.Trim().Equals(itemService.ServiceName.Trim()))
                {
                    return true;
                }
            }
            return false;
        }

        protected void BtnSaveClick(object sender, EventArgs e)
        {
            var service = new Service();
            lbMessage.Text = string.Empty;
            if (serviceBiz.IsServiceNameExist(txtServiceName.Text) <= 0)
            {
                var serviceBenefitsCriterion = string.Empty;
                var serviceBarriersCriterion = string.Empty;
                var serviceEthnicityCriterion = string.Empty;
                var serviceDisabilityCriterion = string.Empty;
                var servicePersonalCircumtancesCriterion = string.Empty;
                var otherServiceParticipationCriterion = string.Empty;
                var clientSuportProcess = string.Empty;
                var intervention = string.Empty;
                var clientJourney = string.Empty;
                var clientOutcome = string.Empty;
                var targetClient = string.Empty;
                var accreditations = string.Empty;
                var referalSources = string.Empty;
                var contractOutcome = string.Empty;
                var contractObligation = string.Empty;

                service.ServiceName = txtServiceName.Text;
                service.ServiceShortDescription = txtShortDescription.Text;
                service.SubType = cboSubType.SelectedValue;
                service.ServiceFullDescription = txtServiceFullDescription.Text;
                service.DeptCode = txtDeptCode.Text;
                service.ContactId = Int32.Parse(hdnContactId.Value);
                service.ServiceType = cboServiceType.SelectedValue;
                service.ClientDescription = txtClientDescription.Text;
                service.ServiceDescriptionDelivery = txtServiceDescriptionDelivery.Text;
                service.ServiceAttendance = cboServiceAttendance.SelectedValue;
                service.ServiceContactCode = txtServiceContractCode.Text;
                service.ServiceStartExpected = txtServiceStartExpected.Text.Equals("")
                                                   ? DateTime.Now
                                                   : Convert.ToDateTime(txtServiceStartExpected.Text);
                service.ServiceContactValue = txtServiceContractValue.Text;
                var serviceStartDate = new DateTime();
                if (txtServiceStartDate.Text.Equals(""))
                {
                    service.ServiceStartDate = null;
                }
                else
                {
                    serviceStartDate = Convert.ToDateTime(txtServiceStartDate.Text);
                    if (serviceStartDate <= DateTime.Now)
                    {
                        lbValidateStartDate.Text =
                            "Service Start Date must be greater than the current date.";
                    }
                    else
                    {
                        lbValidateStartDate.Text = string.Empty;
                        service.ServiceStartDate = serviceStartDate;
                    }
                }
                if (txtServiceEndDate.Text.Equals(""))
                {
                    service.ServiceEndDate = null;
                }
                else
                {
                    var serviceEndDate = Convert.ToDateTime(txtServiceEndDate.Text);
                    if (serviceEndDate <= serviceStartDate)
                    {
                        lbValidateServiceEndDate.Text =
                            "Service End Date must be greater the Service Start Date.";
                    }
                    else
                    {
                        lbValidateServiceEndDate.Text = string.Empty;
                        service.ServiceEndDate = serviceEndDate;
                    }
                }

                service.ReferralProcessMethod = cboReferralProcessMethod.SelectedValue;
                service.ServiceExtendableYear = Convert.ToInt32(cboExtendableYear.SelectedValue);
                service.ServiceExtendableMonth = Convert.ToInt32(cboExtendableMonth.SelectedValue);
                service.ServiceTimeLimitedYear = Convert.ToInt32(cboTimeLimitedYear.SelectedValue);
                service.ServiceTimeLimitedMonth = Convert.ToInt32(cboTimeLimitedMonth.SelectedValue);
                foreach (ListItem item in chkServiceBenefitsCriterion.Items)
                {
                    if (item.Selected)
                    {
                        serviceBenefitsCriterion += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkServiceBarriersCriterion.Items)
                {
                    if (item.Selected)
                    {
                        serviceBarriersCriterion += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkServiceEthnicityCriterion.Items)
                {
                    if (item.Selected)
                    {
                        serviceEthnicityCriterion += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkServiceDisabilitiesCriterion.Items)
                {
                    if (item.Selected)
                    {
                        serviceDisabilityCriterion += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkServicePersonalCircumtancesCriterion.Items)
                {
                    if (item.Selected)
                    {
                        servicePersonalCircumtancesCriterion += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkOtherServiceParticipationCriterion.Items)
                {
                    if (item.Selected)
                    {
                        otherServiceParticipationCriterion += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkClientSuportProcess.Items)
                {
                    if (item.Selected)
                    {
                        clientSuportProcess += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkIntervention.Items)
                {
                    if (item.Selected)
                    {
                        intervention += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkClientJourney.Items)
                {
                    if (item.Selected)
                    {
                        clientJourney += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkClientOutcome.Items)
                {
                    if (item.Selected)
                    {
                        clientOutcome += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkTargetClient.Items)
                {
                    if (item.Selected)
                    {
                        targetClient += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkAccreditations.Items)
                {
                    if (item.Selected)
                    {
                        accreditations += item.Value + ";";
                    }
                }
                foreach (ListItem item in chkReferalSources.Items)
                {
                    if (item.Selected)
                    {
                        referalSources += item.Value + ";";
                    }
                }
                foreach (ListItem item in cblContractOutcome.Items)
                {
                    if (item.Selected)
                    {
                        contractOutcome += item.Value + ";";
                    }
                }
                foreach (ListItem item in cblContractObligation.Items)
                {
                    if (item.Selected)
                    {
                        contractObligation += item.Value + ";";
                    }
                }
                service.ServiceBenefitsCriterion = serviceBenefitsCriterion;
                service.ServiceBarriersCriterion = serviceBarriersCriterion;
                service.ServiceEthnicityCriterion = serviceEthnicityCriterion;
                service.ServiceDisabilityCriterion = serviceDisabilityCriterion;
                service.ServicePersonalCircumtancesCriterion = servicePersonalCircumtancesCriterion;
                service.OtherServiceParticipationCriterion = otherServiceParticipationCriterion;
                service.ClientSuportProcess = clientSuportProcess;
                service.Intervention = intervention;
                service.ClientJourney = clientJourney;
                service.ClientOutcome = clientOutcome;
                service.TargetClient = targetClient;
                service.Accreditations = accreditations;
                service.ReferalSources = referalSources;
                service.Programme = cboProgramme.SelectedValue;
                service.ContractOutcome = contractOutcome;
                service.ContractObligation = contractObligation;
                service.Participation = ddlParticipation.SelectedValue;
                service.FundingSource = ddlFundingSource.SelectedValue;
                service.FundingNeeds = txtFundingNeed.Text;
                service.FundingContactDetails = txtContact1.Text;
                service.FundingAmount = txtFundingAmount.Text;
                service.FundingContinuationAmount = txtFCAmount.Text;
                service.FundingContinuationDetails = txtFCDetail.Text;
                if (txtFundingStart.Text.Equals(string.Empty))
                {
                    service.FundingStart = null;
                }
                else
                {
                    service.FundingStart = Convert.ToDateTime(txtFundingStart.Text);
                }
                if (txtFundingEnd.Text.Equals(string.Empty))
                {
                    service.FundingEnd = null;
                }
                else
                {
                    service.FundingEnd = Convert.ToDateTime(txtFundingEnd.Text);
                }

                service.FundraisingForText = txtFundraisingForText.Text;
                service.FundraisingNeeds = txtFundraisingNeeds.Text;
                if (txtFundraisingRequired.Text.Equals(string.Empty))
                {
                    service.FundraisingRequiredBy = null;
                }
                else
                {
                    service.FundraisingRequiredBy = Convert.ToDateTime(txtFundraisingRequired.Text);
                }
                if (txtFundraisingComplete.Text.Equals(string.Empty))
                {
                    service.FundraisingCompletedDate = null;
                }
                else
                {
                    service.FundraisingCompletedDate = Convert.ToDateTime(txtFundraisingComplete.Text);
                }
                if (txtFDonationDate.Text.Equals(string.Empty))
                {
                    service.FundraisingDonationDate = null;
                }
                else
                {
                    service.FundraisingDonationDate = Convert.ToDateTime(txtFDonationDate.Text);
                }
                service.FundraisingWhy = txtFundraisingWhy.Text;

                service.FundraisingDonorAmount = txtFDonorAmount.Text;

                service.FundraisingDonationIncremental = chkFDonationIncremental.Checked;
                service.IsActive = chkServiceActive.Checked;
                if (serviceBiz.AddServices(service))
                {
                    if (Session.Contents["contact"] != null)
                    {
                        Session["contact"] = null;
                    }
                    if (Session.Contents["contact1"] != null)
                    {
                        Session["contact1"] = null;
                    }
                    lbMessage.Text = "<h3 style='text-align: center'>Insert Successfull.<h2>";
                    ClearControl(this);
                }
            }
            else
            {
                lbMessage.Text = "Service Name already exist ! Please input another service name.";
            }
            
        }

        protected void ClearControl(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                    ClearControl(c);
                else
                {
                    if (c is TextBox)
                        (c as TextBox).Text = "";

                    if (c is CheckBox)
                        (c as CheckBox).Checked = false;

                    if (c is DropDownList)
                        (c as DropDownList).SelectedIndex = 0;
                    if (c is CheckBoxList)
                        (c as CheckBoxList).SelectedIndex = 0;
                }
            }
        }

        protected void cboServiceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckTabs();
        }

        protected void BtnChooseStartExpectedClick(object sender, EventArgs e)
        {
            if (calServiceStartExpected.Visible == false)
            {
                calServiceStartExpected.Visible = true;
                btnChooseStartExpected.Text = "-";
            }
            else
            {
                calServiceStartExpected.Visible = false;
                btnChooseStartExpected.Text = "+";
            }
        }

        protected void CalServiceStartExpectedSelectionChanged(object sender, EventArgs e)
        {
            txtServiceStartExpected.Text = calServiceStartExpected.SelectedDate.ToString("MM/dd/yyyy");
            calServiceStartExpected.Visible = false;
            btnChooseStartExpected.Text = "+";
        }

        protected void BtnChooseStartDateClick(object sender, EventArgs e)
        {
            if (calServiceStartDate.Visible == false)
            {
                calServiceStartDate.Visible = true;
                btnChooseStartDate.Text = "-";
            }
            else
            {
                calServiceStartDate.Visible = false;
                btnChooseStartDate.Text = "+";
            }
        }

        protected void CalServiceStartDateSelectionChanged(object sender, EventArgs e)
        {
            txtServiceStartDate.Text = calServiceStartDate.SelectedDate.ToString("MM/dd/yyyy");
            calServiceStartDate.Visible = false;
            btnChooseStartDate.Text = "+";
        }

        protected void BtnChooseEndDateClick(object sender, EventArgs e)
        {
            if (calServiceEndDate.Visible == false)
            {
                calServiceEndDate.Visible = true;
                btnChooseEndDate.Text = "-";
            }
            else
            {
                calServiceEndDate.Visible = false;
                btnChooseEndDate.Text = "+";
            }
        }

        protected void CalServiceEndDateSelectionChanged(object sender, EventArgs e)
        {
            txtServiceEndDate.Text = calServiceEndDate.SelectedDate.ToString("MM/dd/yyyy");
            calServiceEndDate.Visible = false;
            btnChooseEndDate.Text = "+";
        }

        protected void ChkServiceExtendableCheckedChanged(object sender, EventArgs e)
        {
            if (chkServiceExtendable.Checked)
            {
                cboExtendableYear.Enabled = true;
                cboExtendableMonth.Enabled = true;
            }
            else
            {
                cboExtendableYear.Enabled = false;
                cboExtendableMonth.Enabled = false;
            }
        }

        protected void ChkServiceTimeLimitedCheckedChanged(object sender, EventArgs e)
        {
            if (chkServiceTimeLimited.Checked)
            {
                cboTimeLimitedYear.Enabled = true;
                cboTimeLimitedMonth.Enabled = true;
            }
            else
            {
                cboTimeLimitedYear.Enabled = false;
                cboTimeLimitedMonth.Enabled = false;
            }
        }

        protected void chkFContinuationNeeded_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFContinuationNeeded.Checked)
            {
                txtFCAmount.Enabled = true;
                txtFCDetail.Enabled = true;
            }
            else
            {
                txtFCAmount.Enabled = false;
                txtFCDetail.Enabled = false;
            }
        }

        protected void BtnFundingStartClick(object sender, EventArgs e)
        {
            if (calFundingStart.Visible == false)
            {
                calFundingStart.Visible = true;
                btnFundingStart.Text = "-";
            }
            else
            {
                calFundingStart.Visible = false;
                btnFundingStart.Text = "+";
            }
        }

        protected void CalFundingStartSelectionChanged(object sender, EventArgs e)
        {
            txtFundingStart.Text = calFundingStart.SelectedDate.ToString("MM/dd/yyyy");
            calFundingStart.Visible = false;
            btnFundingStart.Text = "+";
        }

        protected void BtnFundingEndClick(object sender, EventArgs e)
        {
            if (calFundingEnd.Visible == false)
            {
                calFundingEnd.Visible = true;
                btnFundingEnd.Text = "-";
            }
            else
            {
                calFundingEnd.Visible = false;
                btnFundingEnd.Text = "+";
            }
        }

        protected void CalFundingEndSelectionChanged(object sender, EventArgs e)
        {
            txtFundingEnd.Text = calFundingEnd.SelectedDate.ToString("MM/dd/yyyy");
            calFundingEnd.Visible = false;
            btnFundingEnd.Text = "+";
        }

        protected void BtnFundraisingRequiredClick(object sender, EventArgs e)
        {
            if (calFundraisingReqiured.Visible == false)
            {
                calFundraisingReqiured.Visible = true;
                btnFundraisingRequired.Text = "-";
            }
            else
            {
                calFundraisingReqiured.Visible = false;
                btnFundraisingRequired.Text = "+";
            }
        }

        protected void CalFundraisingReqiuredSelectionChanged(object sender, EventArgs e)
        {
            txtFundraisingRequired.Text = calFundraisingReqiured.SelectedDate.ToString("MM/dd/yyyy");
            calFundraisingReqiured.Visible = false;
            btnFundraisingRequired.Text = "+";
        }

        protected void BtnFundraisingCompleteClick(object sender, EventArgs e)
        {
            if (calFundraisingComplete.Visible == false)
            {
                calFundraisingComplete.Visible = true;
                btnFundraisingComplete.Text = "-";
            }
            else
            {
                calFundraisingComplete.Visible = false;
                btnFundraisingComplete.Text = "+";
            }
        }

        protected void CalFundraisingCompleteSelectionChanged(object sender, EventArgs e)
        {
            txtFundraisingComplete.Text = calFundraisingComplete.SelectedDate.ToString("MM/dd/yyyy");
            calFundraisingComplete.Visible = false;
            btnFundraisingComplete.Text = "+";
        }

        protected void ChkFundraisingDonorCheckedChanged(object sender, EventArgs e)
        {
            txtFDonorAmount.Enabled = chkFundraisingDonor.Checked;
        }

        protected void BtnFDonationDateClick(object sender, EventArgs e)
        {
            if (calFDonationDate.Visible == false)
            {
                calFDonationDate.Visible = true;
                btnFDonationDate.Text = "-";
            }
            else
            {
                calFDonationDate.Visible = false;
                btnFDonationDate.Text = "+";
            }
        }

        protected void CalFDonationDateSelectionChanged(object sender, EventArgs e)
        {
            txtFDonationDate.Text = calFDonationDate.SelectedDate.ToString("MM/dd/yyyy");
            calFDonationDate.Visible = false;
            btnFDonationDate.Text = "+";
        }

    }
}